package com.rockcent.service.impl;

import com.rockcent.common.common.BusinessException;
import com.rockcent.helper.MerchantFundFlowrHelper;
import com.rockcent.mall.lib.domain.Mall;
import com.rockcent.mall.lib.domain.Merchant;
import com.rockcent.mall.lib.domain.MerchantRechargeFlow;
import com.rockcent.mall.lib.domain.enumclass.MerchantPayType;
import com.rockcent.mall.lib.domain.enumclass.MerchantRechargeStatus;
import com.rockcent.mall.lib.domain.enumclass.MerchantRechargeType;
import com.rockcent.mall.lib.dto.MerchantRechargeFlowDto;
import com.rockcent.mall.lib.repository.MallRepository;
import com.rockcent.mall.lib.repository.MerchantRechargeFlowRepository;
import com.rockcent.service.MerchantFundSrv;
import com.rockcent.service.MerchantRechargeFlowSrv;
import com.rockcent.service.MerchantSrv;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * B端充值
 * Created by zhangjh on 2017/3/29.
 */
@Service
public class MerchantRechargeFlowSrvImpl implements MerchantRechargeFlowSrv {
    private final Logger log = LoggerFactory.getLogger(MerchantRechargeFlowSrvImpl.class);

    @Autowired
    private MerchantRechargeFlowRepository merchantRechargeFlowRepository;

    @Autowired
    private MerchantFundSrv merchantFundSrv;


    @Autowired
    private MerchantSrv merchantSrv;

    @Autowired
    private MallRepository mallRepository;


    /**
     * b端充值
     *
     * @param rechargeFlow 充值记录
     */
    @Override
    public MerchantRechargeFlow save(MerchantRechargeFlow rechargeFlow) {
        if (null == rechargeFlow) {

        }
        Long mallId = rechargeFlow.getMallId();
        Long merchantId = rechargeFlow.getMerchantId();

        MerchantRechargeStatus status = rechargeFlow.getRechargeStatus();
        BigDecimal rechargeAmount = rechargeFlow.getRechargeAmount();
        String rechargeVoucher = rechargeFlow.getRechargeVoucher();
        String rechargeAccount = rechargeFlow.getRechargeAccount();
        String bankName = rechargeFlow.getBankName();
        String accountName = rechargeFlow.getRechargeAccountName();
        //支付类型
//        MerchantFundFlowType fundFlowType = rechargeFlow.getFundFlowType();
        MerchantPayType payType = rechargeFlow.getPayType();
        if (null == rechargeAmount || rechargeAmount.compareTo(BigDecimal.valueOf(0l)) < 0) {
            throw new BusinessException("充值金额不正确");
        }

        if (null == mallId) {
            throw new BusinessException("平台id为空");
        }

        if (null == merchantId) {
            throw new BusinessException("商户id为空");
        }

        if (null == status) {
            throw new BusinessException("充值状态为空");
        }

        if (MerchantRechargeStatus.ALL == status) {
            throw new BusinessException("充值状态不正确");
        }

        if (null == payType) {
            throw new BusinessException("支付类型");
        }

        if (MerchantPayType.ALL == payType) {
            throw new BusinessException("支付类型不正确");
        }

//        if (MerchantFundFlowType.ONLINE == Type) {
//
//
//        } else
        if (MerchantPayType.OUTLINE == payType) {
            if (StringUtils.isBlank(rechargeAccount)) {
                throw new BusinessException("账号为空");
            }
            if (StringUtils.isBlank(bankName)) {
                throw new BusinessException("银行名称为空");
            }

            if (StringUtils.isBlank(accountName)) {
                throw new BusinessException("户名为空");
            }
        }


//        if (StringUtils.isBlank(rechargeFlow.getRemark())) {
//            rechargeFlow.setRemark(payType.getValue());
//        }


        if (StringUtils.isBlank(rechargeFlow.getSeqNo())) {
            String seqNo = MerchantFundFlowrHelper.SINGLETON.createMerchantFundSeqNo(payType);
            rechargeFlow.setSeqNo(seqNo);
        }


        log.info("======================>flow:", rechargeFlow);
        MerchantRechargeFlow merchantRechargeFlow =  merchantRechargeFlowRepository.save(rechargeFlow);

        merchantFundSrv.recharge(rechargeFlow);

        return  merchantRechargeFlow;
    }

    @Override
    public void saveOnlineRecharge(MerchantRechargeFlowDto dto, Long mallId, Long merchantId, Long operatorId) {


        String seqNo = dto.getSeqNo();
        BigDecimal rechargeAmount = dto.getRechargeAmount();

        if (dto == null) {
            throw new BusinessException("充值信息为空");
        }


        if (null == merchantId) {
            throw new BusinessException("商户id为空");
        }

        if (null == mallId) {
            throw new BusinessException("平台id为空");
        }

        if (rechargeAmount == null) {
            throw new BusinessException("充值金额为空");
        }

        if (rechargeAmount.compareTo(BigDecimal.valueOf(0)) < 0) {
            throw new BusinessException("充值金额必须大于0元");
        }


        if (StringUtils.isBlank(seqNo)) {
            throw new BusinessException("订单号为空");
        }
        Merchant merchant = merchantSrv.findOne(merchantId);

        Mall mall = mallRepository.findOne(mallId);


        if (null == merchant) {
            throw new BusinessException("商户不存在");
        }

        if (null == mall) {
            throw new BusinessException("平台不存在");
        }


        MerchantRechargeFlow merchantRechargeFlow = new MerchantRechargeFlow();
        merchantRechargeFlow.setMerchantId(merchantId);
        merchantRechargeFlow.setMallId(mallId);
        merchantRechargeFlow.setRechargeStatus(MerchantRechargeStatus.SUCCESS);
        merchantRechargeFlow.setPayType(MerchantPayType.ONLINE);
        merchantRechargeFlow.setRemark(dto.getRemark());
        merchantRechargeFlow.setRechargeAmount(dto.getRechargeAmount());
        merchantRechargeFlow.setRechargeType(MerchantRechargeType.RECHARGE_COMMON);
        merchantRechargeFlow.setOperatorId(operatorId);
        merchantRechargeFlow.setSeqNo(dto.getSeqNo());
        save(merchantRechargeFlow);
    }
}
